package com.xiansi.controller;

import com.xiansi.pojo.Adminrole;
import com.xiansi.pojo.Admins;
import com.xiansi.service.AdminsService;
import com.xiansi.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("admin")
@ResponseBody
public class AdminsController {

    @Resource
    private AdminsService adminsService;


    @RequestMapping("login/{name}/{password}")
    public ResultVO<Admins> login(@PathVariable("name") String loginName, @PathVariable("password") String password, HttpServletRequest request) {
        List<Admins> admins = adminsService.login(loginName, password);
        if (admins.size() > 0) {
            for (Admins admin : admins) {
                if (admin != null) {
                    //保存登录信息
                    request.getSession().setAttribute("admin", admin);
                    return new ResultVO<>(admin);
                }
            }
        }
        return new ResultVO<>(500, "用户名或密码不正确");
    }

    /**
     * 根据roleid查询信息
     * @param id
     * @return
     */
    @RequestMapping("role/{id}")
    public ResultVO<Adminrole> queryById(@PathVariable("id") Integer id) {
        Adminrole adminrole = adminsService.queryAdminroleById(id);
        return new ResultVO<>(adminrole);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("loginOut")
    public ResultVO<Admins> loginOut(HttpServletRequest request) {
        request.getSession().invalidate();
        return new ResultVO<>();
    }

}
